#include <i386/mman.h>
#include <i386/bootmem.h>
#include <i386/buddy.h>
#include <i386/memory.h>
#include <i386/page.h>
#include <xbook/debug.h>
#include <const.h>

void init_mman()
{
    /* 初始化物理内存 */
    unsigned int total_size = get_ram_size();
    /* 最低要求内存 */
    if (total_size < MIN_RAM_SIZE)
        panic("Oh! The memory size(%dMB) on your computer is too small,"
            "please make sure it's %dMB at least!\n", total_size / MB, MIN_RAM_SIZE / MB);

    /* 内存区域分为2个区域，固定映射区域和动态映射区域。
    固定映射区域是为内核的运转服务的，动态映射区域是为线程的运转服务的。 */
    unsigned int static_size;
    unsigned int dynamic_size;
    
    static_size = (total_size - (STATIC_MEM_ADDR)) / 2; 
    dynamic_size = total_size - static_size;
    if (static_size > 1*GB) {
        unsigned int more_size = static_size - 1*GB;
        /* 挪出多余的给用户 */
        dynamic_size += more_size;
        static_size -= more_size;
    }
    /* 进行静态内存映射 */
    page_map_static_mem(STATIC_MEM_ADDR, STATIC_MEM_ADDR + static_size);
    /* 根据物理内存大小对内存分配器进行限定 */
    init_boot_mem(KERNEL_VADDR_OFFSET + STATIC_MEM_ADDR , KERNEL_VADDR_OFFSET + (STATIC_MEM_ADDR + static_size));
}
